/*
 * $Id: example.cpp,v 1.7 2004/06/28 00:25:33 sbooth Exp $ 
 *
 * Skeleton of a CGI application written using the GNU cgicc library
 */

#include <exception>
#include <iostream>

#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"
#include <v8.h>

using namespace v8;
using namespace std;
using namespace cgicc;

int
main(int argc, 
     char **argv)
{
  try {
    Cgicc cgi;
    
    // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info
    cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
    cout << html().set("lang", "en").set("dir", "ltr") << endl;

    // Set up the page's header and title.
    cout << head() << endl;
    cout << title() << "GNU cgicc v" << cgi.getVersion() << title() << endl;
    cout << head() << endl;
    
    // Start the HTML body
    cout << body() << endl;

    // Print out a message
    cout << h1("Hello, world from GNU cgicc") << endl;

    // Close the document
    cout << body() << html();
    
    // Create a stack-allocated handle scope.
	HandleScope handle_scope;
	
	// Create a new context.
	Persistent<Context> context = Context::New();
	
	// Enter the created context for compiling and
	// running the hello world script.&nbsp;
	Context::Scope context_scope(context);
	
	// Create a string containing the JavaScript source code.
	Handle<String> source = String::New("'Hello' + ', World!'");
	
	// Compile the source code.
	Handle<Script> script = Script::Compile(source);
				 
	// Run the script to get the result.
	Handle<Value> result = script->Run();
				
	// Dispose the persistent context.
	context.Dispose();
	
	// Convert the result to an ASCII
	//  string and print it.
	String::AsciiValue ascii(result);
	cout << *ascii;
  }
  
  catch(const exception& e) {
    // handle error condition
  }
  
  
  
  return 0;
}
